package com.duqc.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import static java.sql.DriverManager.getConnection;

/**
 * @ClassName JdbcConnectionUtil
 * @Description JdbcConnectionUtil
 * @Author duqc
 * @Date 2023/2/10
 **/
@Slf4j
@Component
public class JdbcConnectionUtil {

    @Value("${spring.datasource.dynamic.datasource.doris.url}")
    private String url;

    @Value("${spring.datasource.dynamic.datasource.doris.username}")
    private String userName;

    /**
     * 获取数据
     *
     * @param commentSql sql
     * @return String
     */
    public List<String> getData(String commentSql) {
        try (Connection connection = getConnection(url, userName, null);
             PreparedStatement preparedStatement = connection.prepareStatement(commentSql)) {
            ResultSet resultSet = preparedStatement.executeQuery();
            List<String> list = new ArrayList<>();
            while (resultSet.next()) {
                list.add(resultSet.getString("attribute_value"));
            }
            return list;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

}
